package cn.eonshine.cuba.lsms.modular.biz.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.biz.entity.TrainGps;
import cn.eonshine.cuba.lsms.modular.CommonService;
import cn.eonshine.cuba.lsms.modular.biz.entity.ETrainExecutionInformation;
import cn.eonshine.cuba.lsms.modular.biz.service.IETrainExecutionService;
import cn.eonshine.cuba.lsms.modular.biz.service.ITranGPSService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;

@Service
public class TranGPSService extends CommonService<TrainGps> implements ITranGPSService {

	@Autowired
	private IETrainExecutionService iETrainExecutionService;

	private final String listUrl = "/trainGps/list";
	private final String updateUrl = "/trainGps/update";
	
	@Override
	public void requestDMZData() {
		requestData(listUrl, updateUrl, InfStatus.INF_LOG_TYPE.TREN_GPS_NUM);
	}

	@Override
	protected Object dataHandle(TrainGps trainGps) throws Exception {
		QueryWrapper<ETrainExecutionInformation> entity = new QueryWrapper<ETrainExecutionInformation>();
//		entity.eq("TRAIN_NO",trainGps.getTrainId()).and("create_date={0}", trainGps.getDataInputDatetime());
		entity.lambda().eq(ETrainExecutionInformation::getTrainNo, trainGps.getTrainId());
		entity.lambda().eq(ETrainExecutionInformation::getCreateDate, trainGps.getDataInputDatetime());
		ETrainExecutionInformation info = iETrainExecutionService.getOne(entity);
		boolean isInsert = false;
		if (info == null) {
			info = new ETrainExecutionInformation();
			isInsert = true;
		}
		
		BeanUtil.copyProperties(trainGps, info,true, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
		info.setTrainNo(trainGps.getTrainId());
		info.setCreateBy(InfStatus.CREATE_BY);
		
		info.setDataTime(trainGps.getCreateDate());
		if(info.getCreateDate() == null) {
			info.setCreateDate(trainGps.getDataInputDatetime());
		}
		info.setUpdateDate(trainGps.getDataInputDatetime());
		info.setUpdateBy(InfStatus.UPDATE_BY);
		
		ETrainExecutionInformation updateEntity = new ETrainExecutionInformation();
		updateEntity.setIsHis("1");
		updateEntity.setTrainNo(info.getTrainNo());
		UpdateWrapper<ETrainExecutionInformation> wrapper = new UpdateWrapper<ETrainExecutionInformation>();
		wrapper.eq("TRAIN_NO", info.getTrainNo());
		wrapper.eq("IS_HIS", "0");
		iETrainExecutionService.update(updateEntity, wrapper);
		
		if(isInsert) {
			iETrainExecutionService.save(info);
		}else {
			iETrainExecutionService.updateById(info);
		}
		
		return info;
	}

	@Override
	protected List<TrainGps> feadBackDataHandle(Object obj, TrainGps trainGps, List<TrainGps> feadBackDataLists) throws Exception {
		ETrainExecutionInformation info = (ETrainExecutionInformation) obj;
		TrainGps tmpBack = new TrainGps();
		tmpBack.setId(info.getId());
		tmpBack.setTrainId(trainGps.getTrainId());
		tmpBack.setDataInputDatetime(trainGps.getDataInputDatetime());
		tmpBack.setLsmsUpdate(BigDecimal.valueOf(InfStatus.LSMS_UPDATE_EXTRACT_YES));
		feadBackDataLists.add(tmpBack);
		return feadBackDataLists;
	}

}
